<?php

namespace Wonfu\Utils;

/**
 * Description of Tokeniz
 *
 * @author vison.cao <visonforcoding@gmail.com>
 */
class Tokeniz
{

    /**
     * 
     * @param \ReflectionMethod $reflectionMethod
     * @return string
     */
    public function extractMethodCode(\ReflectionMethod $reflectionMethod): string
    {
        $filename = $reflectionMethod->getFileName();
        $start_line = $reflectionMethod->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
        $end_line = $reflectionMethod->getEndLine();
        $length = $end_line - $start_line;
        $source = file($filename);
        $body = implode("", array_slice($source, $start_line, $length));
        return $body;
    }

    public function prepareTestFile($namespace)
    {
        $namespace = new \PhpParser\Builder\Namespace_($namespace);
        $namespace->addStmt(new Use_('Tests\TestCase', Node\Stmt\Use_::TYPE_NORMAL));

        return $namespace->getNode();
    }

}
